package cn.iocoder.yudao.module.member.controller.admin.confadditionalfee;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.member.controller.admin.confadditionalfee.vo.MemberConfAdditionalFeePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.confadditionalfee.vo.MemberConfAdditionalFeeRespVO;
import cn.iocoder.yudao.module.member.controller.admin.confadditionalfee.vo.MemberConfAdditionalFeeSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confadditionalfee.MemberConfAdditionalFeeDO;
import cn.iocoder.yudao.module.member.service.memberconf.confadditionalfee.MemberConfAdditionalFeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 阶梯费用配置")
@RestController
@RequestMapping("/member/conf-point-conf-additional-fee")
@Validated
public class MemberConfAdditionalFeeController {

    @Resource
    private MemberConfAdditionalFeeService confPointConfAdditionalFeeService;

    @PostMapping("/create")
    @Operation(summary = "创建阶梯费用配置")
    @PreAuthorize("@ss.hasPermission('member:conf-point-conf-additional-fee:create')")
    public CommonResult<Long> createConfPointConfAdditionalFee(@Valid @RequestBody MemberConfAdditionalFeeSaveReqVO createReqVO) {
        return success(confPointConfAdditionalFeeService.createConfPointConfAdditionalFee(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新阶梯费用配置")
    @PreAuthorize("@ss.hasPermission('member:conf-point-conf-additional-fee:update')")
    public CommonResult<Boolean> updateConfPointConfAdditionalFee(@Valid @RequestBody MemberConfAdditionalFeeSaveReqVO updateReqVO) {
        confPointConfAdditionalFeeService.updateConfPointConfAdditionalFee(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除阶梯费用配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:conf-point-conf-additional-fee:delete')")
    public CommonResult<Boolean> deleteConfPointConfAdditionalFee(@RequestParam("id") Long id) {
        confPointConfAdditionalFeeService.deleteConfPointConfAdditionalFee(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得阶梯费用配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:conf-point-conf-additional-fee:query')")
    public CommonResult<MemberConfAdditionalFeeRespVO> getConfPointConfAdditionalFee(@RequestParam("id") Long id) {
        MemberConfAdditionalFeeDO confPointConfAdditionalFee = confPointConfAdditionalFeeService.getConfPointConfAdditionalFee(id);
        return success(BeanUtils.toBean(confPointConfAdditionalFee, MemberConfAdditionalFeeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得阶梯费用配置分页")
    @PreAuthorize("@ss.hasPermission('member:conf-point-conf-additional-fee:query')")
    public CommonResult<PageResult<MemberConfAdditionalFeeRespVO>> getConfPointConfAdditionalFeePage(@Valid MemberConfAdditionalFeePageReqVO pageReqVO) {
        PageResult<MemberConfAdditionalFeeDO> pageResult = confPointConfAdditionalFeeService.getConfPointConfAdditionalFeePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MemberConfAdditionalFeeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出阶梯费用配置 Excel")
    @PreAuthorize("@ss.hasPermission('member:conf-point-conf-additional-fee:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfPointConfAdditionalFeeExcel(@Valid MemberConfAdditionalFeePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MemberConfAdditionalFeeDO> list = confPointConfAdditionalFeeService.getConfPointConfAdditionalFeePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "阶梯费用配置.xls", "数据", MemberConfAdditionalFeeRespVO.class,
                        BeanUtils.toBean(list, MemberConfAdditionalFeeRespVO.class));
    }

}